/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.allscore.filemonitor.Util;

import java.io.File;
import java.util.Date;

/**
 * @author Administrator
 */
public class FileChanged {

    private String infile;
    boolean flag = true;

    public FileChanged(String infile) {
        super();
        this.infile = infile;
    }

    public void run() {
        File file = new File(infile);
        long s1 = file.lastModified();

        while (flag) { //一直监视  
            try {
                Thread.sleep(1000);
                long s2 = file.lastModified();
                Date date2 = new Date(s2);
                if (s1 != s2) {        //判断一秒钟前后的最后修改时间是否相等  
                    System.out.println("文件已被修改,修改时间为：" + date2);
                    s1 = s2;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        FileChanged jc = new FileChanged("E:\\logs\\aa.txt");
        jc.run();
    }

}
